﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace AspDataBinding
{
    public partial class MobileForm : System.Web.UI.Page
    {
        private SortedDictionary<string, List<string>> cars;
        private List<string> VolkswagenModels;
        private List<string> BMWModels;
        private List<string> MercedesModels; 

        protected void Page_Load(object sender, EventArgs e)
        {           
            VolkswagenModels = new List<string>() { "Golf", "Polo", "Passat" };
            BMWModels = new List<string>() { "316", "318", "520" };
            MercedesModels = new List<string>() { "c220", "s250" };
            cars = new SortedDictionary<string, List<string>>()
            {
                {"Volkswagen",VolkswagenModels},
                {"BMW",BMWModels},
                {"Mercedes",MercedesModels}
            };
            List<string> extras = new List<string>() { "climatic", "airbag", "el.windows", "navigation" };
            List<string> engines = new List<string>() { "gasoline", "diesel", "electric", "hybrid" };
            if (Page.IsPostBack)
            {
                return;
            } 
            
            this.DropDownListType.DataSource = cars.Keys;
            this.DropDownListType.DataBind();
            this.DropDownListType.SelectedIndex = 0;
            this.DropDownListModel.DataSource = cars[DropDownListType.SelectedValue];
            this.DropDownListModel.DataBind();
            this.DropDownListModel.SelectedIndex = 0;


            this.CheckBoxListExtras.DataSource = extras;
            this.CheckBoxListExtras.DataBind();
            this.RadioButtonEngines.DataSource = engines;
            this.RadioButtonEngines.DataBind();
            this.RadioButtonEngines.SelectedIndex = 0;

        }
        protected void DropDownListType_SelectionChanged(object sender, EventArgs e)
        {
            this.DropDownListModel.DataSource = cars[DropDownListType.SelectedValue];
            this.DropDownListModel.DataBind();
        }
        protected void DropDownListModel_SelectionChanged(object sender, EventArgs e)
        {

        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            StringBuilder result = new StringBuilder();
            result.AppendLine(
                "Type: <b>" + DropDownListType.SelectedValue + "</b><br/>");
            result.AppendLine(
                "Model: <b>" + DropDownListModel.SelectedValue + "</b><br/>");
            result.AppendLine("Extras:");
            for (int i = 0; i < CheckBoxListExtras.Items.Count; i++)
            {
                if (CheckBoxListExtras.Items[i].Selected)
                {
                    result.AppendLine(
                        "<b>" + CheckBoxListExtras.Items[i].Text + ",</b>");
                }
            }
            result.AppendLine("<br/>");
            result.AppendLine(
                "Engine: <b>" + RadioButtonEngines.SelectedItem.Text +"</b> <br/>");
            this.LiteralResult.Text = result.ToString();
        }


    }
}